home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / apps / Save8 / Save8.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  7KB  |  313 lines

  1. /* save8.c  7/92   C. Scheppner CBM
  2.  *
  3.  * requires linkage with several iffp modules - see Makefile
  4.  */
  5. #define INTUI_V36_NAMES_ONLY
  6.  
  7. #include "iffp/ilbmapp.h"
  8.  
  9. #ifdef __SASC
  10. void __chkabort(void) {}          /* Disable SAS CTRL-C checking. */
  11. #else
  12. #ifdef LATTICE
  13. void chkabort(void) {}            /* Disable LATTICE CTRL-C checking */
  14. #endif
  15. #endif
  16.  
  17. void chkmsg(void);
  18. void cleanup(void);
  19. void bye(UBYTE *s,int error);
  20.  
  21. #define SAVECHANGES
  22.  
  23. #define MINARGS 2
  24.  
  25. #include "save8_rev.h"
  26. UBYTE vers[] = VERSTAG;
  27. UBYTE Copyright[] = VERS " Save 8bit RGB demo - Freely Redistributable";
  28.  
  29. char *usage = "Usage: save8 savename [grayscale]\n";
  30.  
  31. char *savename;
  32.  
  33. struct Library *IntuitionBase  = NULL;
  34. struct Library *GfxBase        = NULL;
  35. struct Library *IFFParseBase   = NULL;
  36.  
  37. /* Note - these fields are also available in the ILBMInfo structure */
  38. struct   Screen         *scr;         /* for ptr to screen structure */
  39. struct   Window         *win;         /* for ptr to window structure */
  40. struct   RastPort       *wrp;         /* for ptr to RastPort  */
  41. struct   ViewPort       *vp;          /* for ptr to Viewport  */
  42.  
  43. struct   IntuiMessage   *msg;
  44.  
  45. struct   NewWindow      mynw = {
  46.    0, 0,                                  /* LeftEdge and TopEdge */
  47.    0, 0,                                /* Width and Height */
  48.    (UBYTE)-1, (UBYTE)-1,                  /* DetailPen and BlockPen */
  49.    IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS, /* IDCMP Flags with Flags below */
  50.    WFLG_BACKDROP | WFLG_BORDERLESS |
  51.    WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
  52.    WFLG_ACTIVATE | WFLG_RMBTRAP,
  53.    NULL, NULL,                            /* Gadget and Image pointers */
  54.    NULL,                                  /* Title string */
  55.    NULL,                                  /* Screen ptr null till opened */
  56.    NULL,                                  /* BitMap pointer */
  57.    50, 20,                                /* MinWidth and MinHeight */
  58.    0 , 0,                                 /* MaxWidth and MaxHeight */
  59.    CUSTOMSCREEN                           /* Type of window */
  60.    };
  61.  
  62.  
  63. BOOL   FromWb, Done, grayscale;
  64.  
  65.  
  66. UBYTE nomem[]  = "Not enough memory\n";
  67. UBYTE noiffh[] = "Can't alloc iff\n";
  68.  
  69. /* our indexes to reference our frames
  70.  * DEFault, and SCReen
  71.  */
  72. #define DEF    0
  73. #define SCR    1
  74. #define UICOUNT 2
  75.  
  76. /* For our ILBM frames */
  77. struct ILBMInfo  *ilbms[UICOUNT]  = { NULL };
  78.  
  79.  
  80. /* 
  81.  * MAIN 
  82.  */
  83. void main(int argc, char **argv)
  84.    {
  85.    UBYTE ans, c;
  86.    BPTR lock;
  87.    LONG saverror;
  88.    USHORT width, height, depth, bw, bh, x, y;
  89.    ULONG  r, g, b;
  90.    ULONG modeid = 0x8004;
  91.    ULONG  co, maxco;
  92.  
  93.    width = 640;
  94.    height = 400;
  95.    depth = 8;
  96.  
  97.    FromWb = argc ? FALSE : TRUE;
  98.    grayscale = FALSE;
  99.  
  100.    if((argc<MINARGS)||(argv[argc-1][0]=='?'))
  101.     {
  102.     printf("%s\n%s\n",Copyright,usage);
  103.         bye("",RETURN_OK);
  104.     }
  105.  
  106.    savename    = argv[1];
  107.    if(argc > 2)  grayscale = TRUE;
  108.  
  109.    /* if dest not clipboard, warn if dest file already exists */
  110.    if(strcmp(savename,"-c"))
  111.     {
  112.     if(lock = Lock(savename,ACCESS_READ))
  113.         {
  114.         UnLock(lock);
  115.         printf("Dest file \"%s\" already exists.  Overwrite (y or n) ? ",
  116.             savename);
  117.         ans = 0;
  118.         while((c = getchar()) != '\n') if(!ans)  ans = c | 0x20;
  119.         if(ans == 'n')   bye("Exiting.\n",RETURN_OK);
  120.         }
  121.     }
  122.         
  123.    /* Open Libraries */
  124.  
  125.    if(!(IntuitionBase = OpenLibrary("intuition.library", 39)))
  126.       bye("Can't open V39 intuition library.\n",RETURN_WARN);
  127.       
  128.    if(!(GfxBase = OpenLibrary("graphics.library",0)))
  129.       bye("Can't open graphics library.\n",RETURN_WARN);
  130.  
  131.    if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  132.       bye("Can't open iffparse library.\n",RETURN_WARN);
  133.  
  134.  
  135.  
  136. /* 
  137.  * Alloc two ILBMInfo structs (one each for defaults, screen) 
  138.  */
  139.     if(!(ilbms[0] = (struct ILBMInfo *)
  140.     AllocMem(UICOUNT * sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  141.         bye(nomem,RETURN_FAIL);
  142.     else 
  143.     {
  144.     ilbms[SCR] = ilbms[0] + 1;
  145.     }
  146.  
  147. /*
  148.  * Here we set up default ILBMInfo fields for our
  149.  * application's frames.
  150.  
  151.     ilbms[DEF]->windef    = &mynw;
  152. /* 
  153.  * Initialize our working ILBM frames from our default one
  154.  */
  155.     *ilbms[SCR] = *ilbms[DEF];    /* for our screen */
  156.  
  157. /* 
  158.  * Alloc two IFF handles (one for screen frame, one for brush frame) 
  159.  */
  160.     if(!(ilbms[SCR]->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  161.  
  162.     if(!(opendisplay(ilbms[SCR], width, height, depth, modeid)))
  163.     bye("failed to open display",RETURN_FAIL);
  164.  
  165.     /* These were set up by our successful showilbm() above */
  166.     win = ilbms[SCR]->win;    /* our window */
  167.     wrp = ilbms[SCR]->wrp;    /* our window's RastPort */
  168.     scr = ilbms[SCR]->scr;    /* our screen */
  169.     vp  = ilbms[SCR]->vp;    /* our screen's ViewPort */
  170.  
  171.     ScreenToFront(scr);
  172.  
  173.  
  174.  /* Now let's load colors and render some stuff
  175.   */
  176.  
  177.     maxco = vp->ColorMap->Count;
  178.     bh = height / 40;
  179.     bw = width / maxco;
  180.     r = g = b = 0;
  181.  
  182.     if(!grayscale)
  183.         {
  184.     for(x=64, co=0; co < maxco; co++, x+=bw)
  185.             {
  186.         if(co)
  187.             {
  188.                 r = co;
  189.                 g = (maxco - 1) - co;
  190.                 b = (maxco - 1) - ABS((LONG)g-(LONG)r);
  191.             }
  192.         
  193.             r = (r << 24) | (r << 16) | ( r << 8) | r;
  194.             g = (g << 24) | (g << 16) | ( g << 8) | g;
  195.             b = (b << 24) | (b << 16) | ( b << 8) | b;
  196.  
  197.         D(bug("x=%ld, co=%ld, R:G:B = $%02lx:%02lx:%02lx\n",x,co,r,g,b));
  198.  
  199.             if(GfxBase->lib_Version >= 39)
  200.             SetRGB32(vp,co,r,g,b);
  201.         else
  202.             SetRGB4(vp,co,r,g,b);
  203.         SetAPen(wrp,co);
  204.         RectFill(wrp,x,0,x+bw-1,height-1);
  205.         }
  206.     }
  207.    else
  208.         {
  209.         /* 256 Gray scale */
  210.         x = 0;
  211.         y = 0;
  212.         r = 0x00000000;
  213.  
  214.         for(x=64, co=0; co<maxco; co++, x+=bw, r+=0x01010101)
  215.         {
  216.         if(GfxBase->lib_Version >= 39)
  217.         SetRGB32(vp,co,r,r,r);
  218.         else 
  219.         SetRGB4(vp,co,r,r,r);
  220.         SetAPen(wrp,co);
  221.         RectFill(wrp,x,0,x+bw-1,height-1);
  222.         }
  223.         }
  224.  
  225.  
  226.     if(saverror = screensave(ilbms[SCR], ilbms[SCR]->scr,
  227.                 NULL, NULL,
  228.                 savename))
  229.             printf("%s\n",IFFerr(saverror));
  230.  
  231.    Done = FALSE;
  232.    while(!Done)
  233.       {
  234.       Wait(1<<win->UserPort->mp_SigBit);
  235.       chkmsg();
  236.       }
  237.  
  238.  
  239.    cleanup();
  240.    exit(RETURN_OK);
  241.    }
  242.  
  243.  
  244. void chkmsg(void)
  245.     {
  246.     ULONG class;
  247.     UWORD code;
  248.     WORD  mousex, mousey;
  249.  
  250.     while(msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  251.     {
  252.     class = msg->Class;
  253.           code  = msg->Code;
  254.           mousex = msg->MouseX;
  255.           mousey = msg->MouseY;
  256.  
  257.           ReplyMsg(msg);
  258.           switch(class)
  259.            {
  260.         case IDCMP_MOUSEBUTTONS:
  261.         switch(code)
  262.         {
  263.         /* emulate a close gadget */
  264.         case SELECTDOWN:
  265.            if((mousex < 12)&&(mousey < 12))    Done = TRUE;
  266.            break;
  267.         default:
  268.            break;
  269.         }
  270.             case IDCMP_VANILLAKEY:
  271.             switch(code)
  272.                    {
  273.         /* also quit on CTRL-C, CTRL-D, or q */
  274.                    case 'q': case 0x04: case 0x03:
  275.                   Done = TRUE;
  276.                   break;
  277.             
  278.                    default:
  279.                   break;
  280.         }
  281.             default:
  282.             break;
  283.             }
  284.           }
  285.     }
  286.  
  287.  
  288. void bye(UBYTE *s,int error)
  289.    {
  290.    if((*s)&&(!FromWb)) printf("%s\n",s);
  291.    cleanup();
  292.    exit(error);
  293.    }
  294.  
  295.  
  296. void cleanup()
  297.    {
  298.    if(ilbms[SCR])
  299.     {
  300.        if(ilbms[SCR]->scr)        unshowilbm(ilbms[SCR]);
  301.        if(ilbms[SCR]->ParseInfo.iff)    FreeIFF(ilbms[SCR]->ParseInfo.iff);
  302.     }
  303.  
  304.    if(ilbms[0])
  305.     {
  306.     FreeMem(ilbms[0],UICOUNT * sizeof(struct ILBMInfo));
  307.     }
  308.  
  309.    if(GfxBase)          CloseLibrary(GfxBase);
  310.    if(IntuitionBase) CloseLibrary(IntuitionBase);
  311.    if(IFFParseBase)  CloseLibrary(IFFParseBase);
  312.    }
  313.